using Admin2505.Application.Common;
using Admin2505.Application.Dto;
using Admin2505.Domain.Entityes.App;
using Admin2505.Domain.Reposoitories;

namespace Admin2505.Application.Services;

public class RoleService : IRoleService


{
    private readonly IRepository<AppRole> _roleRole;
    private readonly IRepository<Apppermission> _permissionRepo;

    public RoleService(IRepository<AppRole> roleRole,IRepository<Apppermission> permissionRepo)
    {
        _roleRole =roleRole;
        _permissionRepo=permissionRepo;

    }

    

    public async Task<ApiResult> AssignPermissonToRoleAsync(Guid roleId, string PermissionCode)
    {
        var role =await _roleRole.GetByIdAsync(roleId);
        if (role==null)
        {
            return ApiResult.Fail(3033,"角色未找到");
        }
        if (role.IsActived==false)
        {
            return ApiResult.Fail(3034,"角色未启用");
        }
        var permission =(await _permissionRepo.GetAllAsync()).FirstOrDefault(x=>x.Code==PermissionCode); 
        if (permission == null)
        {
            return ApiResult.Fail(3305,"权限未找到");
        }
        if (permission.IsActived==false)
        {
            return ApiResult.Fail(3306,"权限为启用");
        }
        role.GrantPermission(permission);
        await _roleRole.UpdateAsync(role);
        return ApiResult.Sucess(role,"分配权限成功");
    }

    public Task<ApiResult> CreateRoleAsync(CreateRoleDto createRoleDto)
    {
        
        var exists = _roleRole.GetAllAsync().Result.Any(r=>r.RoleName==createRoleDto.RoleName);
        if(exists) 
        {
            return Task.FromResult(ApiResult.Fail(3001,"角色名已存在"));

        }
        var role = new AppRole (createRoleDto.RoleName);
        _roleRole.CreateAsync(role).Wait();
        return Task.FromResult(ApiResult.Sucess(role,"创建角色成功"));
    }

    public Task<ApiResult> DeleteRoleAsync(Guid roleId)
    {
        var role =_roleRole.GetByIdAsync(roleId).Result;
        if (role==null)
        {
            return Task.FromResult(ApiResult.Fail(3302,"角色未找到"));

        }
        _roleRole.DeleteAsync(roleId).Wait();
        return Task.FromResult(ApiResult.Sucess(role ,"删除角色成功"));
    }

    public Task<ApiResult> DisableAsync(Guid roleId)
    {
        var role=_roleRole.GetByIdAsync(roleId).Result;
        if (role==null)
        {
            return Task.FromResult(ApiResult.Fail(3302,"角色未找到"));
        }
        if (!role.IsActived)
        {
            return Task.FromResult(ApiResult.Fail(3302,"角色已禁用"));
        }
        role.IsActived=false;
        _roleRole.UpdateAsync(role).Wait();
        return Task.FromResult(ApiResult.Sucess(role,"禁用角色成功"));
    }

    public Task<ApiResult> EnableAsync(Guid roleId)
    {
        var role= _roleRole.GetByIdAsync(roleId).Result;
        if(role==null)
        {
            return Task.FromResult(ApiResult.Fail(3302,"角色未找到"));
        }
        if (role.IsActived)
        {
            return Task.FromResult(ApiResult.Fail(3303,"角色已启用"));

        }
        role.IsActived=false;
        _roleRole.UpdateAsync(role).Wait();
        return Task.FromResult(ApiResult.Sucess(3304,"启用角色成功"));
    }

    public Task<ApiResult> RemovePermissionFromRoleAsync(Guid roleId, string PermissionCode)
    {
       var role= _roleRole.GetByIdAsync(roleId).Result;
       if(role==null)
       {
            return Task.FromResult(ApiResult.Fail(3302,"角色未找到"));
       }
       var Permission =  _permissionRepo.GetAllAsync().Result.FirstOrDefault(x=>x.Code==PermissionCode);
       if(Permission==null)
       {
        return Task.FromResult(ApiResult.Fail(3303,"权限未找到"));
       }
       role.RevokePermission(Permission);
       _roleRole.UpdateAsync(role).Wait();
       return Task.FromResult(ApiResult.Sucess(3304,"移除权限成功"));
    }

    public Task<ApiResult> UpdateRoleAsync(Guid roleId,CreateRoleDto createRoleDto)
    {
        var role =_roleRole.GetByIdAsync(roleId).Result;
        if (role==null)
        {
            return Task.FromResult(ApiResult.Fail(3302,"角色未找到"));
        }
        role.RoleName = createRoleDto.RoleName;
        _roleRole.UpdateAsync(role).Wait();
        return Task.FromResult(ApiResult.Sucess(role,"更新成功"));
    }
}
